package ysq.xyg.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.model.*;
import ysq.xyg.model.info.BaseUserInfo;
import ysq.xyg.model.info.CompensateInfo;
import ysq.xyg.model.info.Page;
import ysq.xyg.model.info.ReturnInfo;
import ysq.xyg.service.CompensateService;
import ysq.xyg.service.CustomService;
import ysq.xyg.service.StoreService;
import ysq.xyg.service.UseDoService;
import ysq.xyg.util.BaseConstant;
import ysq.xyg.util.BaseUtil;
import ysq.xyg.util.DateUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.Date;
import java.util.List;

/**
 * @author yu sheng qiang
 * @create 2017-04-19 14:15
 **/
@Controller
@RequestMapping("/compensate")
public class CompensateController extends BaseController {
    private static Logger log= LoggerFactory.getLogger(CompensateController.class);
    @Resource
    private CompensateService compensateService;
    @Resource
    private CustomService customService;
    @Resource
    private UseDoService useDoService;
    @Resource
    private StoreService storeService;
    private Compensate compensate;

    @RequestMapping("/listPage")
    public ModelAndView List(HttpServletRequest request, HttpServletResponse response){
        return new ModelAndView("/compensateList");
    }

    @RequestMapping(value = "/jsonList",method = RequestMethod.POST)
    public @ResponseBody JSONObject JsonList(HttpServletRequest request,Compensate compensate, Page page) throws MalformedURLException, UnsupportedEncodingException {
        compensate.setRemoveTag("N");
        PageInfo<Compensate> pageInfo=compensateService.pageLeft(compensate,page.getLimit(),page.getPage());
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("msg","");
        jsonObject.put("count",pageInfo.getTotal());
        jsonObject.put("data",pageInfo.getList());
        return jsonObject;
    }

    @RequestMapping(value = "/selectEntity",method = RequestMethod.GET)
    public ModelAndView SelectEntity(HttpServletRequest request,String id){
        ModelAndView view=new ModelAndView("/compensateUpdateOrSave");
        //1、总管理员，2、财务号，3、店铺管理员
        int kind=acceptCurrentUserKind(request);
        view.addObject("kind",kind);
        Compensate compensate=compensateService.selectByPrimaryKey(id);
        List<Store> storeList=storeService.list(new Store());
        List<Custom> customList=customService.list(new Custom());
        view.addObject("customList",customList);
        view.addObject("storeList",storeList);
        view.addObject("compensate",compensate);
        return view;
    }

    @RequestMapping(value = "/updateOrAddEntity",method = RequestMethod.POST)
    public @ResponseBody JSONObject UpdateOrAddEntity(HttpServletRequest request,CompensateInfo compensateInfo){
        BaseUserInfo baseUserInfo=acceptCurrentUser(request);
        ReturnInfo returnInfo=new ReturnInfo();
        JSONObject jsonObject=new JSONObject();
        if(compensateInfo.getId()==null || "".equals(compensateInfo.getId())){
            compensate=new Compensate();
            compensate.setId(BaseUtil.uId());
            compensate.setCreateTime(new Date());
            compensate.setCustomId(compensateInfo.getCustomId());
            compensate.setWangWang(compensateInfo.getWangWang());
            compensate.setOrderNum(compensateInfo.getOrderNum());
            compensate.setWeChat(compensateInfo.getWeChat());
            compensate.setMoney(compensateInfo.getMoney());
            compensate.setReason(compensateInfo.getReason());
            compensate.setSkm(compensateInfo.getSkm());
            compensate.setBackMoney(compensateInfo.getBackMoney());
            compensate.setRemoveTag("N");
            compensate.setReturnBank(compensateInfo.getReturnBank());
            if(StringUtil.isNotEmpty(compensateInfo.getReturnDate())){
                compensate.setReturnTime(DateUtil.convertStringToDate(compensateInfo.getReturnDate()));
            }
            compensate.setRemark(compensateInfo.getRemark());
            compensate.setMonth(compensateInfo.getMonth());
            compensate.setUpdateTime(new Date());
            int id=compensateService.insert(compensate);
            if(id <= 0){
                returnInfo=new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_INSERT);
            }else{
                useDoService.insert(new UseDo(baseUserInfo.getUserid(),"添加追加补偿信息["+compensate.toString()+"]",BaseConstant.USE_DO_TYPE_ADD_COMPENSATE));
            }
        }else{
            compensate=compensateService.selectByPrimaryKey(compensateInfo.getId());
            compensate.setCustomId(compensateInfo.getCustomId());
            compensate.setWangWang(compensateInfo.getWangWang());
            compensate.setOrderNum(compensateInfo.getOrderNum());
            compensate.setWeChat(compensateInfo.getWeChat());
            compensate.setMoney(compensateInfo.getMoney());
            compensate.setReason(compensateInfo.getReason());
            compensate.setSkm(compensateInfo.getSkm());
            compensate.setReturnBank(compensateInfo.getReturnBank());
            compensate.setReturnTime(compensateInfo.getReturnTime());
            compensate.setRemark(compensateInfo.getRemark());
            compensate.setMonth(compensateInfo.getMonth());
            compensate.setBackMoney(compensateInfo.getBackMoney());
            compensate.setUpdateTime(new Date());
            if(StringUtil.isNotEmpty(compensateInfo.getReturnDate())){
                compensate.setReturnTime(DateUtil.convertStringToDate(compensateInfo.getReturnDate()));
            }
            int id=compensateService.updateByPrimaryKey(compensate);
            if(id <= 0){
                returnInfo=new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_UPDATE);
            }else{
                useDoService.insert(new UseDo(baseUserInfo.getUserid(),"修改追加补偿信息["+compensate.toString()+"]",BaseConstant.USE_DO_TYPE_UPDATE_COMPENSATE));
            }
        }
        jsonObject.put("returnInfo",returnInfo);
        return jsonObject;
    }
    @RequestMapping(value = "/deleteEntity",method = RequestMethod.GET)
    public @ResponseBody JSONObject DeleteEntity(HttpServletRequest request,String id){
        BaseUserInfo baseUserInfo=acceptCurrentUser(request);
        ReturnInfo returnInfo=new ReturnInfo();
        JSONObject jsonObject=new JSONObject();
        if(StringUtil.isEmpty(id)){
            returnInfo=new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_CANSHU);
        }else{
            Compensate compensate=compensateService.selectByPrimaryKey(id);
            compensate.setRemoveTag("Y");
            int num=compensateService.updateByPrimaryKey(compensate);
            if(num <= 0){
                returnInfo=new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_DELETE);
            }else{
                useDoService.insert(new UseDo(baseUserInfo.getUserid(),"删除追加补偿信息["+compensate.toString()+"]",BaseConstant.USE_DO_TYPE_DELETE_COMPENSATE));
            }
        }
        jsonObject.put("returnInfo",returnInfo);
        return jsonObject;
    }
    @RequestMapping(value = "/export")
    public void getExport(HttpServletResponse response, Compensate compensate){
        compensate.setRemoveTag("N");
        compensateService.export(response,compensate);
    }
}
